define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/ininventory/index',
                    add_url: 'warehouse/ininventory/add',
                    edit_url: 'warehouse/ininventory/edit',
                    del_url: 'warehouse/ininventory/del',
                    multi_url: 'warehouse/ininventory/multi',
                    table: 'in_inventory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'inorderid', title: __('Inorderid')},
                        {field: 'trackorderid', title: __('Trackorderid'),operate:false},
                        {field: 'actualcount', title: __('Actualcount'),operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'items', title: __('ItemInfo'),operate:false,formatter:Controller.api.formatter.itemFormat,events:Controller.api.events.getItems},
                        {field: 'status', title: __('Status'),formatter: Controller.api.formatter.checkstatus},
                        {field: 'warehouseid', title: __('Warehouseid')},
                        {field: 'remark', title: __('Remark'),operate:false}
                    ]
                ],
                search:false,
                showToggle: false,
                searchFormVisible:true,
                exportDataType: 'selected',
                exportTypes: ['doc', 'excel']
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click','.add-track',function () {
                var that = this;
                var ids = Table.api.selectedids(table);

                var url = "warehouse/ininventory/addTrackNumber";
                ids = ($.isArray(ids) ? ids.join(",") : ids);
                url = Table.api.replaceurl(url, {ids:ids}, table);
                Fast.api.open(url, '关联追踪号', $(that).data() || {});
            });

            $(document).on('click','.print_inventory',function (){
                var that = this;
                var ids = Table.api.selectedids(table);

                var url = "warehouse/ininventory/printInventory?ids="+ids;
                //ids = ($.isArray(ids) ? ids.join(",") : ids);
                //url = Table.api.replaceurl(url,{ids:ids},table); window.alert(url);
                url = Fast.api.fixurl(url);
                //for(var i=3;i>0;i--){
                    //$.get(url);
                    window.location = url;
                //}
                return;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        addtracknumber: function () {
            Controller.api.bindevent();
        },
        printinventory: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                checkstatus: function (value, row, index) {
                    if(value == 2000){
                        return '<span>已审核</span>';
                    }else if(value == 2001){
                        return '<span>已拒绝</span>';
                    }else if(value == 3000){
                        return '<span>已确认</span>';
                    }else{
                        return '<span>审核中</span>';
                    }

                },
                itemFormat: function (value,row,index) {
                    return "<span class='el-tag' title='点击查看所有商品信息'>"+value+"</span>";
                }
            },
            events:{
                getItems:{
                    'click .el-tag': function (e, value, row, index) {
                        e.stopPropagation();
                        Fast.api.ajax({
                                url: 'warehouse/ininventory/items',
                                data: {id: row.id}
                            }, function (data, ret) {
                                var items = "";
                                for(i=0;i<data.length;i++){
                                    items += "<span>"+data[i]['itemname']+" - " + data[i]['itemsku']+ " * "+ data[i]['instoragecount']+"</span><br/>";
                                }
                                Layer.alert(items);
                                return false;
                            },function (data,ret) {
                                alert(ret.msg);
                            }
                        );

                    }
                }
            }
        }
    };
    return Controller;
});